<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var obj = {
            id: 1,
            name: "hobby",
            msg: {
                age: 20
            },
            color: ["pink", "yellow"]
        }
        var o = {}
        // 1.Object.assign 浅拷贝
        Object.assign(o, obj)
        console.log(o);
        o.msg.age = 18
        console.log(o);
        // 2.深拷贝
        function deepcopy(newobj, oldobj) {
            for (k in oldobj) {
                let item = oldobj[k]
                if (item instanceof Array) {
                    newobj[k] = []
                    deepcopy(newobj[k], item)
                } else if (item instanceof Object) {
                    newobj[k] = {}
                    deepcopy(newobj[k], item)
                } else {
                    newobj[k] = item
                }
            }
        }
        deepcopy(o, obj)
        console.log(o);
    </script>
</body>

</html>